package com.lzy.cloud.ticket.admission.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.lzy.cloud.ticket.admission.dtos.ScenicTicketsDto;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

public class JsonUtils {

    public static Map<String, Object> toMap(JsonObject json) {
        Map<String, Object> map = new HashMap();
        Set<Map.Entry<String, JsonElement>> entrySet = json.entrySet();
        Iterator iter = entrySet.iterator();

        while(iter.hasNext()) {
            Map.Entry<String, JsonElement> entry = (Map.Entry)iter.next();
            String key = (String)entry.getKey();
            Object value = entry.getValue();
            if (value instanceof JsonArray) {
                map.put(key, toList((JsonArray)value));
            } else if (value instanceof JsonObject) {
                map.put(key, toMap((JsonObject)value));
            } else {
                map.put(key, value);
            }
        }

        return map;
    }

    public static List<Object> toList(JsonArray json) {
        List<Object> list = new ArrayList();

        for(int i = 0; i < json.size(); ++i) {
            Object value = json.get(i);
            if (value instanceof JsonArray) {
                list.add(toList((JsonArray)value));
            } else if (value instanceof JsonObject) {
                list.add(toMap((JsonObject)value));
            } else {
                list.add(value);
            }
        }

        return list;
    }

}
